from sqlalchemy import Column, String, Text, Enum, ForeignKey, DateTime
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
import uuid
from datetime import datetime, timezone
from enum import Enum as PyEnum

from db.base import Base

class RiskLevel(str, PyEnum):
    """风险等级枚举"""
    HIGH = 'high'
    MEDIUM = 'medium'
    LOW = 'low'

class RemediationStatus(str, PyEnum):
    """整改状态枚举"""
    IN_PROGRESS = 'in_progress'  # 整改中
    COMPLETED = 'completed'      # 已完成

class RemediationItem(Base):
    """Database model for remediation items."""
    __tablename__ = "remediations"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    item_id = Column(UUID(as_uuid=True), ForeignKey("compliance_item.id"), nullable=False, index=True)
    plan_id = Column(UUID(as_uuid=True), ForeignKey("assessment_plan.id"), nullable=False, index=True)
    title = Column(String(255), nullable=False)
    description = Column(Text, nullable=True)
    suggestion = Column(Text, nullable=False)
    risk_level = Column(String(50), nullable=False)
    status = Column(String(50), nullable=False, default=RemediationStatus.IN_PROGRESS)
    completion_date = Column(DateTime(timezone=True), nullable=True)
    created_at = Column(DateTime(timezone=True), nullable=False, default=datetime.now(timezone.utc))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))

    # Relationships
    compliance_item = relationship("ComplianceItem", back_populates="remediations")
    assessment_plan = relationship("AssessmentPlan", back_populates="remediations")
